package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.core.net.toUri
import com.google.gson.Gson
import com.luck.picture.lib.PictureSelector
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.adapter.CarAdapter
import com.stars.home.base.MBActivity
import com.stars.home.config.MIntentAction
import com.stars.home.databinding.ActivityAuthDriverBinding
import com.stars.home.inte.OnUploadCallback
import com.stars.home.model.AttesDetail
import com.stars.home.model.AttestationDetail
import com.stars.home.model.TypeGet
import com.stars.home.model.bean.UpImgBean
import com.stars.home.utils.FileUtils
import com.stars.home.utils.GridSpaceItemDecoration
import com.stars.home.utils.OssUtils
import com.stars.home.vmodel.AuthDriverVModel
import com.tencent.mmkv.MMKV
import com.trailer.base.utils.UiUtils
import java.util.*


/**
 *on 2021/5/9
 */
class AuthDriverActivity : MBActivity<AuthDriverVModel, ActivityAuthDriverBinding>(), OnUploadCallback<UpImgBean> {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        binding.recyclerCar.addItemDecoration(GridSpaceItemDecoration(2,
                UiUtils.dp2Px(this, 22f),
                UiUtils.dp2Px(this, 9f)))



        if(!TextUtils.isEmpty(vm.attestation.value)&&(vm.attestation.value.equals("1")
                        ||vm.attestation.value.equals("2"))){
           //获取详情
               if(vm.attestation.value.equals("1")){
                   vm.isTips.value=true
               }

            vm.getTypeGet(1)

        }else{
            vm.getTypeGet(2)
        }
        if(vm.attestation.value.equals("1")){
            vm.getUserInfo()
        }

        if(vm.attestation.value.equals("0")){
            //没有提交过
           var ss= MMKV.defaultMMKV().getString("item", "")

            if(!TextUtils.isEmpty(ss)){

                vm.getTypeGet(0)

            }
        }

        vm.listCar.observe(this, androidx.lifecycle.Observer {
            if(null!=it){
                iniCarData(it)
            }
        })

        vm.listCarTo.observe(this, androidx.lifecycle.Observer {
            if(null!=it){
                var ss= MMKV.defaultMMKV().getString("item", "")
                var to= MMKV.defaultMMKV().getString("to","")
                val data1: AttestationDetail = Gson().fromJson(ss, AttestationDetail::class.java)
                val data2: AttesDetail = Gson().fromJson(to, AttesDetail::class.java)
                vm.setInitData(data1,data2,it)
                iniCarData(it)
            }
        })

        vm.listCarTwo.observe(this, androidx.lifecycle.Observer {
            iniCarData(it)
        })


    }

    private fun iniCarData(list:List<TypeGet>) {
        //adapter
        var adapter= CarAdapter(vm)
        binding.recyclerCar.adapter= adapter
        adapter.setList(list)

        adapter.setOnItemClickListener { adapter, view, position ->
            var item= list[position]
            for(ss in list){
                ss.chose = ss.id==item.id
                if(ss.chose){
                    vm.type_id=ss.id
                }
            }
            adapter.notifyDataSetChanged()

        }

    }

    override fun vmClass(): Class<AuthDriverVModel> {
        return AuthDriverVModel::class.java
    }
    override fun binding(): ActivityAuthDriverBinding {
        return ActivityAuthDriverBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "司机认证"
    }

    override fun listeners() {
        super.listeners()
        binding.llSubmit.onClick {
            //提交信息
            vm.toSubmit()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_THREE) {//图片

            val selectList = PictureSelector.obtainMultipleResult(data)
            val list = ArrayList<String>()
            if (null != selectList) {
                if (selectList[0].path.contains("content://")) {

                    val cr = this.contentResolver
                    list.add(FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(), cr))

                } else {
                    list.add(selectList[0].path)
                }

                when (vm.type) {
                    1 -> vm.isOne.value = true
                    2 -> vm.isTwo.value = true
                    3 -> vm.isThree.value = true
                    4 -> vm.isFour.value = true
                    5->vm.isFive.value=true
                }

                OssUtils(this, vm)
                        .setUpLoadMore("car", list, vm.type, this)


            }

            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            if (selectList[0].path.contains("content://")) {

                val cr = this.contentResolver
                list.add(FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(), cr))

            } else {
                list.add(selectList[0].path)
            }

            when (vm.type) {
                1 -> vm.isOne.value = true
                2 -> vm.isTwo.value = true
                3 -> vm.isThree.value = true
                4 -> vm.isFour.value = true
                5->vm.isFive.value=true
            }
            OssUtils(this, vm)
                    .setUpLoadMore("car", list, vm.type, this)


            return
        }
    }

    override fun onUploadSuccess(t: UpImgBean) {
       var index = t.position

        when (index) {
            1 -> {
                vm.identityZ.value = t.url
                vm.identityZ_up=t.urlPath
                //vm.discernID(ss[1])
                vm.isOne.value = false

            }
            2 -> {
                vm.identityF.value = t.url
                vm.identityF_up=t.urlPath
                vm.isTwo.value = false
            }

            3 -> {
                vm.drivingZ.value = t.url
                vm.drivingZ_up=t.urlPath
                vm.isThree.value = false

            }
            4 -> {
                vm.drivingF.value =t.url
                vm.drivingF_up=t.urlPath
                vm.isFour.value = false
            }
            5 -> {
                Log.e("--shy-", "t.url=: "+t.url )
                vm.carImg.value = t.url
                vm.carImg_up = t.urlPath
                vm.isFive.value = false
            }


        }
    }

    override fun onUploadError(error: String?) {
    }

    override val isRxbus: Boolean
        get() = true

}
